<%@page import="org.apache.commons.lang3.StringUtils"%>
<%@page import="fi.foyt.cs.persistence.domainmodel.locale.LocaleText"%>
<%@page import="fi.foyt.cs.persistence.domainmodel.locale.LocaleElement"%>
<%@page import="fi.foyt.cs.controller.ItemController"%>
<%@page import="fi.foyt.cs.Settings"%>
<%@page import="java.util.Locale"%>
<%@page import="java.util.List"%>
<%@page import="fi.foyt.cs.controller.LocaleController"%>
<%@page import="fi.foyt.cs.persistence.domainmodel.items.Item"%>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <jsp:include page="../fragments/head.jsp"></jsp:include>
</head>
<body>
  <%
    LocaleController localeController = new LocaleController();  
    ItemController itemController = new ItemController(); 
    
    List<Locale> supportedLocales = Settings.getSupportedLocales();

    if ("POST".equals(request.getMethod())) {
      int itemCount = Integer.parseInt(request.getParameter("itemCount"));
      for (int i = 0; i < itemCount; i++) {
        Long id = Long.parseLong(request.getParameter("item." + i + ".id"));
        String icon = request.getParameter("item." + i + ".icon");

        Item item = itemController.findItemById(id);
        LocaleElement localeElement = localeController.findLocaleElementById(item.getLocaleElementId());
        
        boolean delete = true;
        for (Locale locale : supportedLocales) {
          String name = request.getParameter("item." + i + ".name_" + locale.getLanguage());
          if (!StringUtils.isBlank(name)) {
            LocaleText localeText = localeController.findLocaleTextByLocaleElementAndLocale(localeElement, locale);    
            
            if (localeText != null) {
              localeController.updateLocaleText(localeText, name);
            } else {
              localeController.createLocaleText(localeElement, locale, name);
            }
            
            delete = false;
          }
        }
        
        if (delete) {
          itemController.deleteItem(item);
        } else {
          itemController.updateIcon(item, icon);
        }
      }

      boolean create = false;
      for (Locale locale : supportedLocales) {
        String name = request.getParameter("item.-1.name_" + locale.getLanguage());
        if (!StringUtils.isBlank(name))
          create = true;
      }
      
      if (create) {
        String icon = request.getParameter("item.-1.icon");
        LocaleElement localeElement = localeController.createLocaleElement();
        itemController.createItem(localeElement, icon);
        
        for (Locale locale : supportedLocales) {
          String name = request.getParameter("item.-1.name_" + locale.getLanguage());
          if (!StringUtils.isBlank(name)) {
            localeController.createLocaleText(localeElement, locale, name);
          }
        }
      }
    }

    pageContext.setAttribute("supportedLocales", supportedLocales);
    pageContext.setAttribute("items", itemController.listAllItems());
  %>

	<jsp:include page="../fragments/header.jsp"></jsp:include>
	<h1>Items</h1>

	<form method="POST">
		<input type="hidden" name="itemCount" value="${fn:length(items)}" />

		<table>
			<tr>
			  <c:forEach items="${supportedLocales}" var="supportedLocale">
          <th>Name - ${supportedLocale}</th>
			  </c:forEach>
			  <th>Icon</th>
			</tr>

			<c:set var="index" value="0" />

			<c:forEach items="${items}" var="item">
				<input type="hidden" name="item.${index}.id" value="${item.id.id}" />
				<tr>
          <c:forEach items="${supportedLocales}" var="supportedLocale">
            <%
              Item item = (Item) pageContext.getAttribute("item");
              LocaleElement localeElement = localeController.findLocaleElementById(item.getLocaleElementId());
              LocaleText localeText = localeController.findLocaleTextByLocaleElementAndLocale(localeElement, (Locale) pageContext.getAttribute("supportedLocale"));
              if (localeText != null)
                pageContext.setAttribute("name", localeText.getText());
            %> 
				    <td><input type="text" value="${name}" name="item.${index}.name_${supportedLocale}" /></td>
          </c:forEach>
          <td><input type="text" value="${item.icon}" name="item.${index}.icon" /></td>
				</tr>
				<c:set var="index" value="${index + 1}" />
			</c:forEach>
			
			<tr>
        <c:forEach items="${supportedLocales}" var="supportedLocale">
          <td><input type="text" name="item.-1.name_${supportedLocale}" /></td>
        </c:forEach>
        <td><input type="text" name="item.-1.icon" /></td>
      </tr>
		</table>

		<input type="submit" value="Save" />
	</form>

	<jsp:include page="../fragments/footer.jsp"></jsp:include>
</body>
</html>